# -*- coding: utf-8 -*-
"""
@Time ： 2022/7/28 20:44
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：第一个Web自动化脚本实战
笔记文档：https://www.yuque.com/docs/share/73a7c983-4fcd-4bcf-8838-de0675b1738a
项目路径：http://47.105.110.138:8000/index.php/Home/user/login.html
"""
import os.path
import re
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

# 创建浏览器对象
# driver = webdriver.Chrome()
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from class8.homework import Web

web = Web()
web.openbrowser('gc')
driver = web.driver

# 添加隐式等待
driver.implicitly_wait(5)

# 打开网站
driver.get('http://47.105.110.138:8000/index.php/Home/user/login.html')

# id定位
# ele = driver.find_element('id','username')
ele = driver.find_element(By.ID, 'username')
# 操作元素
ele.send_keys('13800138006')

ele = driver.find_element('link text', '淘宝网')
print(ele.get_attribute('outerHTML'))

ele = driver.find_element('partial link text', '淘宝')
print(ele.get_attribute('outerHTML'))

# 输入密码
driver.find_element('xpath', '//*[@id="password"]').send_keys('123456')
driver.find_element('xpath', '//*[@id="verify_code"]').send_keys('111')

driver.find_element('partial link text', '登').click()
# driver.execute_script("arguments[0].click()",ele)

# 获取昵称
nickname = driver.find_element('xpath', '//a[@class="red userinfo"]').text
print(nickname)

# 悬停(鼠标移动到元素)
ele = driver.find_element('xpath', '//span[text()="账户设置"]')
# 创建对象
action = ActionChains(driver)
# 鼠标移动到元素上
action.move_to_element(ele)
# 一定要加perform，操作才会生效
action.perform()

# 个人信息
driver.find_element('xpath', '//a[text()="个人信息"]').click()

# 图片上传
driver.find_element('xpath', '//*[@id="preview"]').click()

# 进入iframe
ele = driver.find_element('xpath', '//iframe')
driver.switch_to.frame(ele)

# 获取绝对路径
path = os.path.abspath('../lib/will.png')
print(path)
# # 如果有多个元素，你默认获取到第一个元素
# ele = driver.find_element('xpath','//input[@type="file"]')
# ele.send_keys(path)

# 如果有多个元素，怎么获取指定个数
# # 方式1：(xpath)[2]        （下标从1开始）
# ele = driver.find_element('xpath','(//input[@type="file"])[2]')
# ele.send_keys(path)

# 方式2：获取到所有元素，从元素列表里面去第几个
ele = driver.find_elements('xpath', '//input[@type="file"]')
ele[1].send_keys(path)

# 显示等待
# 等待元素的文本属性包含：已上传1份
# 每隔0.5s判断一次，直到10s，就报错
# 判断条件，是//div[@class="info"]元素的文本内容包含：已上传1份
WebDriverWait(driver, 10, 0.5).until(
    EC.text_to_be_present_in_element((By.XPATH, '//div[@class="info"]'), '已上传1份'))

# 点击确定使用
driver.find_element('xpath', '//div[@class="saveBtn"]').click()
# 退出iframe：都是退出到最外层HTML里面
driver.switch_to.default_content()
driver.find_element('xpath','//input[@class="save"]').click()


WebDriverWait(driver, 10, 0.5).until(
    EC.invisibility_of_element((By.XPATH, '//span[text()="操作成功"]')))

# 搜索
driver.find_element('xpath','//*[@id="q"]').send_keys('手机')
driver.find_element('xpath','//*[@id="sourch_form"]/a').click()


# 点击第一个加入购物出
driver.find_element('xpath','//a[contains(text(),"Huawei/华为 nova 2s 4G全面屏")]/../..//a[text()="加入购物车"]').click()
driver.find_element('id','join_cart').click()
time.sleep(1)
driver.find_element('xpath','//*[@id="layui-layer1"]/span/a').click()

# 结算
driver.find_element('xpath','//*[@id="hd-my-cart"]/a/div/span').click()
driver.find_element('link text','去结算').click()
time.sleep(2)
driver.find_element('class name','checkout-submit').click()

# 获取订单号
text = driver.find_element('xpath','//p[@class="succ-p"]').text
print(text)

orderid = re.findall(r'[0-9]{18}',text)

driver.find_element('xpath','//a[text()="我的订单"]').click()

# 切换窗口
# 获取窗口的标志id列表
handles = driver.window_handles
print(handles)
# # 下标切换
# driver.switch_to.window(handles[1])

# 按标题切换
for h in handles:
    driver.switch_to.window(h)
    if driver.title.__contains__('我的订单'):
        break


driver.find_element('xpath','//*[@id="search_key"]').send_keys(orderid[0])
driver.find_element('xpath','//*[@id="search_order"]/input[2]').click()
